package com.fanshilz.flower2025.framework.util.address;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.json.JSONUtil;
import com.fanshilz.flower2025.business.flower.addressCode.entity.AddressCode;
import com.fanshilz.flower2025.business.flower.addressCode.service.IAddressCodeService;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class AddressUtil {

    public static List<MapAddress> addresses = new ArrayList<>();

    @Resource
    private IAddressCodeService addressCodeService;

    @PostConstruct
    public void init() {
        String data = ResourceUtil.readUtf8Str("pca-code.json");
        addresses = JSONUtil.toList(data, MapAddress.class);
        if (addressCodeService.count() > 0) {
            System.out.println();
        } else {

            List<AddressCode> addressCodes = new ArrayList<>();

            addresses.forEach(p -> {

                String provinceLetter = String.valueOf(PinyinUtil.getFirstLetter(p.getName().charAt(0))).toUpperCase();

                addressCodes.add(AddressCode.builder()
                        .type(0)
                        .provinceName(p.getName())
                        .provinceCode(p.getCode())
                        .provinceLetter(provinceLetter)
                        .build());

                if (CollUtil.isNotEmpty(p.getChildren())) {

                    p.getChildren().forEach(c -> {

                        String cityName = c.getName().equals("市辖区") ? p.getName() : c.getName();

                        String cityLetter = String.valueOf(PinyinUtil.getFirstLetter(cityName.charAt(0))).toUpperCase();

                        addressCodes.add(AddressCode.builder()
                                .type(1)
                                .provinceName(p.getName())
                                .provinceCode(p.getCode())
                                .provinceLetter(provinceLetter)
                                .cityName(cityName)
                                .cityCode(c.getCode())
                                .cityLetter(cityLetter)
                                .build());

                        if (CollUtil.isNotEmpty(c.getChildren())) {
                            c.getChildren().forEach(a -> {

                                String areaLetter = String.valueOf(PinyinUtil.getFirstLetter(a.getName().charAt(0))).toUpperCase();

                                addressCodes.add(AddressCode.builder()
                                        .type(2)
                                        .provinceName(p.getName())
                                        .provinceCode(p.getCode())
                                        .provinceLetter(provinceLetter)
                                        .cityName(cityName)
                                        .cityCode(c.getCode())
                                        .cityLetter(cityLetter)
                                        .areaCode(a.getCode())
                                        .areaName(a.getName())
                                        .areaLetter(areaLetter)
                                        .build());
                            });
                        }
                    });
                }
            });

            addressCodeService.saveBatch(addressCodes);
        }
    }

}
